﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：马毅 
    //
    // 文件名：DAL_Area.cs
    // 文件功能描述：类别数据访问逻辑
    // 创建标识：马毅2009-05-24
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Text;
using HZ.DrugTrade.Model;
using HZ.Common;
using System.Data;
using System.Data.SqlClient;
using HZ.Data;
namespace HZ.DrugTrade.DAL
{
    /// <summary>
    /// 类别数据访问逻辑
    /// </summary>
    public class DAL_Sort
    {
        /// <summary>
        /// 条件类别查询
        /// </summary>
        /// <param name="where">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public static IList<Sort> GetCompanyByWhere(HZ.DrugTrade.Model.Sort query, int currentPage, int pageSize, out int totalRecord)
        {
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Sort>(query,true);

            HZ.Data.PageDescribe pdes = new HZ.Data.PageDescribe(string.Format("SELECT * FROM TB_Sort  WHERE 1=1 {0} ", where), pageSize);
            pdes.CurrentPage = currentPage;
            totalRecord = pdes.TotalCount;

            DataTable dt = pdes.GetSpecailPage();
            return HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.Sort>(dt);
        }

        /// <summary>
        /// 绑定分类名
        /// </summary>
        /// <returns></returns>
        public static IList<string> GetSortName()
        {
            string sql = "SELECT SORTNAME FROM TB_SORT order by sortid";
            IList<string> strlist=new List<string>();
            DataTable dt = HZ.Data.DbHelperFactory.Create().ExecuteTable(sql);
            if (dt != null)
            {
                IList<HZ.DrugTrade.Model.Sort> lst = HZ.Data.DAL_Helper.CommonFillList<HZ.DrugTrade.Model.Sort>(dt);
                foreach (var item in lst)
                {
                    strlist.Add(item.SortName);
                }
            }
            return strlist;
        }
        /// <summary>
        /// 条件类别查询 返回单个实体
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static Sort GetSortwhere(HZ.DrugTrade.Model.Sort query)
        {
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Sort>(query,true);
            // Sort sort = new Sort();
            return HZ.Data.DAL_Helper.GetModelByID(query);
        }
        /// <summary>
        /// 删除类别 
        /// </summary>
        /// <param name="sort">1 删除成功 2 删除失败</param>
        /// <param name="ntype">1 提升 、 2 删除当前节点和他的子节点 </param>
        /// <returns></returns>
        public static int DeleteSortSon(Sort sort, int ntype)
        {
            var h =  HZ.Data.DbHelperFactory.Create();
            try
            {
                h.BeginTrans();
                string sql = "";
                if (ntype == 1)
                {
                    sql = string.Format("UPDATE TB_Sort SET ParentSortID = '{0}' WHERE ParentSortID = '{1}'", sort.ParentSortID, sort.SortID);
                    h.ExecuteNonQuery(sql);
                }
                else
                {
                    sql = string.Format("DELETE FROM TB_Sort WHERE ParentSortID = '{0}'", sort.SortID);
                    h.ExecuteNonQuery(sql);
                    sql = string.Format("DELETE FROM TB_Sort WHERE SortID = '{0}'", sort.SortID);
                    h.ExecuteNonQuery(sql);
                }


                h.CommitTrans();
                return 1;

            }
            catch (Exception ee)
            {
                h.RollTrans();
                return 0;
                throw;

            }

        }
        /// <summary>
        /// 提升
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="strParentSortID"></param>
        /// <returns></returns>
        public static int UPSortSon(Sort sort)
        {
            try
            {
                var h =  HZ.Data.DbHelperFactory.Create();
                string sql = string.Format("update TB_Sort set ParentSortID=(select ParentSortID from TB_Sort  where SortID='{0}') where SortID='{1}'", sort.ParentSortID, sort.SortID);
                h.ExecuteNonQuery(sql);
                return 1;
            }
            catch
            {
                return 0;
            }
        }
    }

}
